/******************************************************************************
 * FILE NAME:
 *  transport_main.h
 *
 * DESCRIPTION:
 * This file contains the function handle for transport layer
 * This file contains following function definitions.
 * 				tp_init_master_thread_func()
 * 				tp_connection_thread_handle_func()
 * 				tp_master_thread_handler_func()
 *				tp_create_connection_thread_func()
 *				tp_rm_connection_thread_func()
 *				tp_init_listenning_thread_func()
 *				tp_listening_thread_handle_func()
 *				initialization_func()
 *
 *
 * DESIGN REFERENCE:
 *
 *
 * REVISION HISTORY   :
 * Date            Author               REASON
 * 02.08.2013      Thuan Nguyen         Creation
 * 07.08.2013      Thuan Nguyen         Add function tp_init_sender_thread_func() and function
 * 										tp_sender_thread_handle_func()
 * Copyright 2005, NET
 * ***************************************************************************/

#include"../h/tp_types.h"
#include"../../common/h/logging.h"
#include"../h/tp_linked_list_def.h"
#include"../../common/h/syslog_api_platform.h"
#include"../h/queue_def.h"
#include"../h/tp_ap_int_def.h"

extern threads_transport_t *g_threads_list;
extern pthread_mutex_t g_thread_list_mutex;

extern U8 g_created_connection_flag;
/* Value 1 (SYSLOG_TRUE)=> waiting create connection
 * Value 0 (SYSLOG_FALSE)=> create connection failure
 * */

U8 tp_init_sender_thread_func();
void tp_sender_thread_handle_func(threads_transport_t *thread_data);
U8 tp_init_master_thread_func();
U8 tp_connection_thread_handle_func(threads_transport_t *thread_info);
void tp_master_thread_handler_func(threads_transport_t *thread_data);
U8 tp_create_connection_thread_func(U32 socket_id,struct sockaddr_in client_addr);
U8 tp_rm_connection_thread_func(request_msg_t *req_msg);
U8 tp_init_listenning_thread_func();
U8 tp_listening_thread_handle_func(threads_transport_t *thread_data);
U8 tp_initialization_func();



/***********************************************************************
 * FUNCTION NAME:
 *  tp_init_sender_thread_func
 *
 * DESCRIPTION:
 * This function will init sender thread. Sender thread will monitor input_queue of
 * transport layer. All message which send to transport layer will into input_queue.
 *
 * INPUT:
 *
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
U8 tp_init_sender_thread_func()
{
	U8                    val_result = SYSLOG_FALSE;
	pthread_t             thread_key;
	void*                 temp_thread_result;
	threads_transport_t   *temp_thread_node = SYSLOG_NULL;

	SYSLOG_TRANSPORT_INFO_LOGGING("Enter tp_init_sender_thread_func");

	/*Create a Node on linked-list and this Node will be parameter of function handle of master function*/
	temp_thread_node = tp_add_empty_item_to_ll_func(g_threads_list);
	if(SYSLOG_NULL == temp_thread_node)
	{
			SYSLOG_TRANSPORT_ERROR_LOGGING("Can't Add a Node to g_threads_list.");
			val_result = SYSLOG_FALSE;
	}
	else
	{
		val_result = pthread_create(&thread_key,NULL,(void*)tp_sender_thread_handle_func \
								,temp_thread_node);
		if(SYSLOG_SUCCESS != val_result)
		{
			SYSLOG_TRANSPORT_ERROR_LOGGING("Can't create sender thread");
			tp_rm_empty_item_from_ll_func(g_threads_list,temp_thread_node);
			val_result = SYSLOG_FALSE;
		}
		else
		{
			temp_thread_node->data_thread.thread_type = SENDER_THREAD_TYPE;
			temp_thread_node->data_thread.thread_key = thread_key;
			temp_thread_node->data_thread.thread_func = (void*)tp_sender_thread_handle_func;
			temp_thread_node->state_thread = ITEM_STATE_ACTIVE;
			val_result = SYSLOG_TRUE;
		}
	}
	SYSLOG_TRANSPORT_INFO_LOGGING("Exit tp_init_sender_thread_func");
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME:
 *  tp_sender_thread_handle_func
 *
 * DESCRIPTION:
 * This function will monitor input_queue of transport and send message to client.
 * Send request to tasklist if need create a new connection.
 *
 * INPUT:
 *	threads_transport_t *thread_data
 * OUTPUT:
 *
 * RETURN:
 *
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
void tp_sender_thread_handle_func(threads_transport_t *thread_data)
{
	SYSLOG_TRANSPORT_INFO_LOGGING("Enter function tp_sender_thread_handle_func");
	/*Waiting state change to Acvite*/
	while(ITEM_STATE_INACTIVE == thread_data->state_thread){}

	/*monitor input_quueue of transport*/
	tp_monitor_input_queues_func();
	SYSLOG_TRANSPORT_INFO_LOGGING("Exit function tp_sender_thread_handle_func");
}
/***********************************************************************
 * FUNCTION NAME: 
 *  tp_initialization_func
 *
 * DESCRIPTION:
 * initial all global variable and other initialization.
 * initial for input_message_queue and task_list
 *
 * INPUT: 
 *	
 * OUTPUT:
 *	
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
U8 tp_initialization_func()
{
	U8           val_result;
	SYSLOG_TRANSPORT_INFO_LOGGING("Enter function tp_initialization_func");
	/*init Mutex for g_threads_list*/
	val_result = pthread_mutex_init(&g_thread_list_mutex,SYSLOG_NULL);
	if(SYSLOG_SUCCESS != val_result)
	{
		SYSLOG_TRANSPORT_ERROR_LOGGING("Mutex Initialization failure!");
		val_result = SYSLOG_FALSE;
		return val_result;
	}
	/*Create TRANSPORT_INPUT_MQUEUE*/
	val_result = tp_create_input_queues_func();
	if(SYSLOG_FAILURE == val_result)
	{
		SYSLOG_TRANSPORT_ERROR_LOGGING("TRANSPORT_INPUT_MQUEUE: msgget failure");
		SYSLOG_TRANSPORT_ERROR_LOGGING("TRANSPORT_INPUT_MQUEUE: Can't create message_queue for transport_input_queue");
		return val_result;
	}
	/*Create TASKLIST*/
	val_result = tp_create_tasklist_queues_func();
	if(SYSLOG_FAILURE == val_result)
	{
		SYSLOG_TRANSPORT_ERROR_LOGGING("TASKLIST: msgget failure");
		SYSLOG_TRANSPORT_ERROR_LOGGING("TASKLIST: Can't create message_queue for task_list");
		return val_result;
	}
	g_created_connection_flag = SYSLOG_TRUE;
	SYSLOG_TRANSPORT_INFO_LOGGING("Exit function tp_initialization_func");
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME: 
 *  tp_init_master_thread_func
 *
 * DESCRIPTION:
 * 	This function will create master threads.
 *
 * INPUT: 
 *	request_msg_t *req_msg
 * OUTPUT:
 *	
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
U8 tp_init_master_thread_func()
{
	U8                      val_result = SYSLOG_FALSE;
	pthread_t               thread_key;
	void                    *temp_thread_result;
	threads_transport_t     *temp_thread_node = SYSLOG_NULL;
	SYSLOG_TRANSPORT_INFO_LOGGING("Enter function tp_init_master_thread_func");
	/*Create a Node on linked-list and this Node will be parameter of function handle of master function*/
	temp_thread_node = tp_add_empty_item_to_ll_func(g_threads_list);
	if(SYSLOG_NULL == temp_thread_node)
	{
			SYSLOG_TRANSPORT_ERROR_LOGGING("Can't Add a Node to g_threads_list.");
			val_result = SYSLOG_FALSE;
	}
	else
	{
		val_result = pthread_create(&thread_key,NULL,(void*)tp_master_thread_handler_func \
								,temp_thread_node);
		if(SYSLOG_SUCCESS != val_result)
		{
			SYSLOG_TRANSPORT_ERROR_LOGGING("Can't create slave thread");
			tp_rm_empty_item_from_ll_func(g_threads_list,temp_thread_node);
			val_result = SYSLOG_FALSE;
		}
		else
		{
			temp_thread_node->data_thread.thread_type = MASTER_THREAD_TYPE;
			temp_thread_node->data_thread.thread_key = thread_key;
			temp_thread_node->data_thread.thread_func = (void*)tp_master_thread_handler_func;
			temp_thread_node->state_thread = ITEM_STATE_ACTIVE;
			val_result = SYSLOG_TRUE;
		}
	}

	/*Join master thread*/
	val_result = pthread_join(thread_key,&temp_thread_result);
	if(SYSLOG_SUCCESS != val_result)
	{
		val_result = SYSLOG_FALSE;
		SYSLOG_TRANSPORT_ERROR_LOGGING("Join thread master failure.");
	}
	SYSLOG_TRANSPORT_INFO_LOGGING("Exit function tp_init_master_thread_func");
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME: 
 *  tp_create_connection_thread_func
 *
 * DESCRIPTION: 
 * 	create connection thread for a connection. This connection will handle all
 *  transfer data corresponding port and ip_address.
 *
 * INPUT: 
 *	socket_id
 *	client_addr
 *
 * OUTPUT:
 *	
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
U8 tp_create_connection_thread_func(U32 socket_id,struct sockaddr_in client_addr)
{
	U8                      val_result = SYSLOG_FALSE;
	pthread_t               thread_key;
	void                    *temp_thread_result;
	threads_transport_t     *temp_thread_node = SYSLOG_NULL;
	thread_info_t           thread_info;

	SYSLOG_TRANSPORT_INFO_LOGGING("Enter function tp_create_connection_thread_func");
	/*Create a Node on linked-list and this Node will be parameter of slave function*/
	temp_thread_node = tp_add_empty_item_to_ll_func(g_threads_list);
	if(SYSLOG_NULL == temp_thread_node)
	{
		SYSLOG_TRANSPORT_ERROR_LOGGING("Can't Add a Node to g_threads_list.");
		val_result = SYSLOG_FALSE;
	}
	else
	{
		val_result = pthread_create(&thread_key,NULL,(void*)tp_connection_thread_handle_func \
					,temp_thread_node);

		if(SYSLOG_SUCCESS != val_result)
		{
			SYSLOG_TRANSPORT_ERROR_LOGGING("Can't create connection thread");
			tp_rm_empty_item_from_ll_func(g_threads_list,temp_thread_node);
			val_result = SYSLOG_FALSE;
		}
		else
		{
			temp_thread_node->data_thread.thread_type = CONNECTION_THREAD_TYPE;
			temp_thread_node->data_thread.thread_key = thread_key;
			temp_thread_node->data_thread.socket_id = socket_id;
			temp_thread_node->data_thread.client_addr = client_addr;
			temp_thread_node->data_thread.thread_func = (void*)tp_connection_thread_handle_func;
			temp_thread_node->state_thread = ITEM_STATE_ACTIVE;



			val_result = SYSLOG_TRUE;
			SYSLOG_TRANSPORT_DEBUG_LOGGING("Create a connection thread with information:" \
					                       "socket_id[%d] ,state_thread[%d]", \
					                       temp_thread_node->data_thread.socket_id,\
					                       temp_thread_node->state_thread);
		}
	}
	SYSLOG_TRANSPORT_INFO_LOGGING("Exit function tp_create_connection_thread_func");
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME: 
 *  tp_init_listenning_thread_func
 *
 * DESCRIPTION: 
 * Initial listening_thread
 *
 * INPUT: 
 *	
 * OUTPUT:
 *	
 * RETURN:
 *	
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
U8 tp_init_listenning_thread_func()
{
	U8                          val_result = SYSLOG_FALSE;
	pthread_t                   thread_key;
	U8                          socket_server;
	void                        *temp_thread_result;
	threads_transport_t         *temp_thread_node = SYSLOG_NULL;
	struct sockaddr_in server;
	
	socket_server = socket(AF_INET,SOCK_STREAM,0);
	
	if(SYSLOG_FAILURE == socket_server)
	{
		SYSLOG_TRANSPORT_ERROR_LOGGING("Can't create socket server");
		return val_result;
	}
	SYSLOG_TRANSPORT_INFO_LOGGING("Create socket id: %d",socket_server);
	U32 lenght;
	struct sockaddr_in temp_addr;
	getsockname(socket_server,(struct sockaddr*)&temp_addr,&lenght);
	SYSLOG_TRANSPORT_INFO_LOGGING("Ip address: %s",inet_ntoa(temp_addr.sin_addr));
	
	/*Initial Socket server*/
	server.sin_family = AF_INET;
	server.sin_port = htons(SYSLOG_PORT_LISTEN);
	server.sin_addr.s_addr = INADDR_ANY;
	
	
	
	/*Create a Node on linked-list and this Node will be parameter of slave function*/
	temp_thread_node = tp_add_empty_item_to_ll_func(g_threads_list);

	if(SYSLOG_NULL == temp_thread_node)
	{
		SYSLOG_TRANSPORT_ERROR_LOGGING("Can't Add a Node to g_threads_list for listening thread.");
		return val_result;
	}
	else
	{
		val_result = pthread_create(&thread_key,NULL,(void*)tp_listening_thread_handle_func \
					,temp_thread_node);
		if(SYSLOG_SUCCESS != val_result)
		{
			SYSLOG_TRANSPORT_ERROR_LOGGING("Can't create listening thread");
			tp_rm_empty_item_from_ll_func(g_threads_list,temp_thread_node);
			val_result = SYSLOG_FALSE;
		}
		else
		{
			temp_thread_node->data_thread.thread_type = LISTEN_THREAD_TYPE;
			temp_thread_node->data_thread.thread_key = thread_key;
			temp_thread_node->data_thread.socket_id = socket_server;
			temp_thread_node->data_thread.client_addr = server;
			temp_thread_node->data_thread.thread_func = (void*)tp_listening_thread_handle_func;
			temp_thread_node->state_thread = ITEM_STATE_ACTIVE;
		}
	}
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME: 
 *  tp_listening_thread_handle_func
 *
 * DESCRIPTION: 
 * This function handle for listening thread. 
 *
 * INPUT: 
 *	request_msg_t *req_msg
 * OUTPUT:
 *	
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
U8 tp_listening_thread_handle_func(threads_transport_t *thread_data){
	U8               val_result=SYSLOG_FALSE;
	U32              read_size;
	U8               socket_server,socket_client;
	struct           sockaddr_in server,client;
	request_msg_t    temp_req_msg;
	U8               client_message[2000];
	
	/*Waiting change state to Active*/
	while(ITEM_STATE_INACTIVE == thread_data->state_thread){}

	socket_server = thread_data->data_thread.socket_id;
	server = thread_data->data_thread.client_addr;
	
	/*Handle message at here.*/
	if(bind(socket_server,(struct sockaddr*)&server,sizeof(server)) < 0)
	{
		SYSLOG_TRANSPORT_ERROR_LOGGING("bind failure!");
	}
	else
	{
		SYSLOG_TRANSPORT_INFO_LOGGING("bind done !");
		listen(socket_server,SYSLOG_PORT_LISTEN);
		SYSLOG_TRANSPORT_INFO_LOGGING("waiting for incoming connection ...");
			
		while(1)
		{
			socket_client = accept(socket_server,(struct sockaddr*)&client,(socklen_t*)sizeof(struct sockaddr_in));
			
			if(socket_client<0){
				SYSLOG_TRANSPORT_ERROR_LOGGING("Accept Failure\n");
				exit(EXIT_FAILURE);
			}
			else
			{
				SYSLOG_TRANSPORT_INFO_LOGGING("Accept successful\n");
				/*Send a request Open a connection_thread for TASKLIST*/
				temp_req_msg.action = OPEN_CONNECTION;
				temp_req_msg.client_addr = client;
				temp_req_msg.socket_id = socket_client;
				temp_req_msg.reasion = MESSAGE_SEND_FROM_INTPUT_QUEUE;
				T_OS_SEND_MSG_QUEUE(TASK_ID_TRANSPORT,TASKLIST_MQUEUE, \
						&temp_req_msg,sizeof(request_msg_t));
			}
		}
	}
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME: 
 *  tp_master_thread_handler_func
 *
 * DESCRIPTION:
 *  This function handle for master thread. Master thread will
 *  monitor TASKLIST. if have any request which was sent to TASKLIST.
 *  Master thread will receive request and execute requests.
 *
 * INPUT: 
 *	request_msg_t *req_msg
 * OUTPUT:
 *	
 * RETURN:
 *
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
void tp_master_thread_handler_func(threads_transport_t *thread_data)
{
	/*Waiting state change to Acvite*/
	while(ITEM_STATE_INACTIVE == thread_data->state_thread){}

	U8 val_result=SYSLOG_FALSE;
	/*Create Listening_thread*/
	tp_init_listenning_thread_func();
	/*Create sender_thread*/
	tp_init_sender_thread_func();
	/*print list of created threads*/
	tp_list_all_thread_into_ll_func();
	/*Monitor tasklist to receive request*/
	tp_monitor_tasklist_queues_func();
}
/***********************************************************************
 * FUNCTION NAME: 
 *  tp_connection_thread_handle_func
 *
 * DESCRIPTION:
 *	Function is handled-function of connection-thread.
 * INPUT: 
 *	request_msg_t *req_msg
 * OUTPUT:
 *	
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *
 * NOTES:
 *
 ************************************************************************/
U8 tp_connection_thread_handle_func(threads_transport_t *thread_info)
{
	U8                   val_result = SYSLOG_FALSE;
	U32                  read_size;
	void*                msg;
	U8                   socket_id;
	struct sockaddr_in   client_addr;
	request_msg_t        temp_req_msg;

	SYSLOG_TRANSPORT_INFO_LOGGING("Enter function tp_connection_thread_handle_func");
	/*Waiting for connection thread change state to Active*/
	while(ITEM_STATE_INACTIVE == thread_info->state_thread){	}

	/*Set cancelstate and canceltype for connection-thread*/
	if(SYSLOG_SUCCESS != pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,SYSLOG_NULL))
	{
		SYSLOG_TRANSPORT_ERROR_LOGGING("setcancelstate with PTHREAD_CANCEL_ENABLE state \
				is failure\n");
	}
	else
	{
		if(SYSLOG_SUCCESS != pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,SYSLOG_NULL))
		{
			SYSLOG_TRANSPORT_ERROR_LOGGING("setcanceltype with PTHREAD_CANCEL_ASYNCHRONOUS \
					state is failure\n");
		}
	}
	/*Socket_id = -1 : mean is that need a new connection to send message and listen on this
	 * socket.
	 * */
	socket_id = thread_info->data_thread.socket_id;

	if(SYSLOG_FAILURE == socket_id)
	{
		//Create a new connection with client_addr.
		socket_id = socket(AF_INET,SOCK_STREAM,0);
		if(SYSLOG_FAILURE != socket_id)
		{

			SYSLOG_TRANSPORT_INFO_LOGGING("Create socket_client id: %d",socket_id);
			/*Init for client_addr*/
			client_addr = thread_info->data_thread.client_addr;
			client_addr.sin_family = AF_INET;

			/*Connect to clients*/
			if(SYSLOG_FAILURE != connect(socket_id,(struct sockaddr*)&client_addr, \
					sizeof(client_addr)))
			{
				SYSLOG_TRANSPORT_INFO_LOGGING("Connected to client");
				thread_info->data_thread.socket_id = socket_id;
				while((read_size == T_OS_RECEI_MSG(TASK_ID_TRANSPORT,socket_id,\
							msg,TRANSPORT_PROTOCOL_TCP))>0)
				{
					SYSLOG_TRANSPORT_INFO_LOGGING("%s",(char*)msg);
					memset(msg,0,TRANSPORT_MESSEGE_MAX_LENGHT);
				}
				if(SYSLOG_FALSE == read_size)
				{
					/*In This Case Client is disconnected with server*/
					temp_req_msg.reasion = DISCONNECT_WITH_CLIENT;
					SYSLOG_TRANSPORT_WARNING_LOGGING("Client Disconnected\n");
				}
				else
				{
					if(SYSLOG_FAILURE == read_size)
					{
						temp_req_msg.reasion = READ_DATA_FAILURE;
						SYSLOG_TRANSPORT_WARNING_LOGGING("Read Failure!\n");
					}
				}
			}
			else
			{
				g_created_connection_flag = SYSLOG_FALSE;
				temp_req_msg.reasion = CANNOT_CONNECT_TO_CLIENT;
				SYSLOG_TRANSPORT_WARNING_LOGGING("Can't connect client");
			}
		}
		else
		{
			g_created_connection_flag = SYSLOG_FALSE;
			temp_req_msg.reasion = CANNOT_CREATE_SOCKET_CLIENT;
			SYSLOG_TRANSPORT_WARNING_LOGGING("Can't create socket_client");
		}
		/*Send close connection request to TASKLIST*/
		temp_req_msg.msg_id = TRANSPORT_REQ_MSG_ID;
		temp_req_msg.action = CLOSE_CONNECTION;
		temp_req_msg.client_addr = thread_info->data_thread.client_addr;
		temp_req_msg.socket_id = thread_info->data_thread.socket_id;
		temp_req_msg.thread_key = thread_info->data_thread.thread_key;
		T_OS_SEND_MSG_QUEUE(TASK_ID_TRANSPORT,TASKLIST_MQUEUE, \
					&temp_req_msg,sizeof(request_msg_t));
	}
	else
	{
		//Execute receive data on this socket.
		while((read_size == T_OS_RECEI_MSG(TASK_ID_TRANSPORT,socket_id,\
						msg,TRANSPORT_PROTOCOL_TCP))>0)
		{
			SYSLOG_TRANSPORT_INFO_LOGGING("%s",(char*)msg);
			memset(msg,0,TRANSPORT_MESSEGE_MAX_LENGHT);
		}
		if(read_size == 0)
		{
			/*In This Case Client is disconnected with server*/
			temp_req_msg.reasion = DISCONNECT_WITH_CLIENT;
			SYSLOG_TRANSPORT_ERROR_LOGGING("Client Disconnected\n");
		}
		else
		{
			if(read_size==-1)
			{
				temp_req_msg.reasion = READ_DATA_FAILURE;
				SYSLOG_TRANSPORT_ERROR_LOGGING("Read Failure!\n");
			}
		}
		/*Send close connection request to TASKLIST*/
		temp_req_msg.msg_id = TRANSPORT_REQ_MSG_ID;
		temp_req_msg.action = CLOSE_CONNECTION;
		temp_req_msg.client_addr = thread_info->data_thread.client_addr;
		temp_req_msg.socket_id = thread_info->data_thread.socket_id;
		temp_req_msg.thread_key = thread_info->data_thread.thread_key;
		T_OS_SEND_MSG_QUEUE(TASK_ID_TRANSPORT,TASKLIST_MQUEUE, \
						&temp_req_msg,sizeof(request_msg_t));
	}
	SYSLOG_TRANSPORT_INFO_LOGGING("Exit function tp_connection_thread_handle_func");
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME: 
 *  tp_rm_connection_thread_func
 *
 * DESCRIPTION:
 *	Begin of Transport Layer
 * INPUT:
 *	U8 socket_id
 *	struct socketaddr_in *client_addr
 * OUTPUT:
 *
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *	init_master_thread_func()
 * NOTES:
 *
 ************************************************************************/
U8 tp_rm_connection_thread_func(request_msg_t *req_msg)
{
	U8                   val_result = SYSLOG_FALSE;
	thread_info_t        temp_thread;

	SYSLOG_TRANSPORT_INFO_LOGGING("Enter function tp_rm_connection_thread_func");
	if(SYSLOG_NULL == req_msg)
	{
		SYSLOG_TRANSPORT_ERROR_LOGGING("Can't Remove connection thread with NULL request msg");
		val_result = SYSLOG_FALSE;
	}
	else
	{

		temp_thread.client_addr = req_msg->client_addr;
		temp_thread.thread_key = req_msg->thread_key;
		temp_thread.socket_id = req_msg->socket_id;

		val_result = tp_rm_thread_from_ll_func(g_threads_list,&temp_thread);
		if(SYSLOG_FALSE == val_result)
		{
			SYSLOG_TRANSPORT_ERROR_LOGGING("Can't Remove connection thread with NULL request msg");
		}
		else
		{
			SYSLOG_TRANSPORT_INFO_LOGGING("Remove connection thread to Linked-list successful");
			/*Kill thread and close socket-id*/
			close(temp_thread.socket_id);
			pthread_cancel(temp_thread.thread_key);
		}
	}
	SYSLOG_TRANSPORT_INFO_LOGGING("Exit function tp_rm_connection_thread_func");
	return val_result;
}
/***********************************************************************
 * FUNCTION NAME:
 *  main
 *
 * DESCRIPTION:
 *	Begin of Transport Layer
 * INPUT: 
 *	U32 argc
 *	U8 **argv
 * OUTPUT:
 *	
 * RETURN:
 *	val_result
 * GLOBAL DATA REFERRED:
 *
 * CALLED ROUTINES:
 *	init_master_thread_func()
 * NOTES:
 *
 ************************************************************************/
U32 main(U32 argc,U8 **argv)
{
	tp_initialization_func();
	tp_init_master_thread_func();
	exit(EXIT_SUCCESS);
}

